package day22;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo {
    public static void main(String[] args) {
        //1.创建线程池
        ExecutorService es  = Executors.newSingleThreadExecutor();
        long start = System.currentTimeMillis();
        //2.执行线程池
        for (int j = 0; j < 10; j++) {
            es.execute(() -> {
                String name = Thread.currentThread().getName();
                System.out.println(name + "开始进洞");
                for (int i = 3; i > 0; i--) {
                    System.out.println(name + "还需要" + i + "S");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(name + "开始出洞");
            });
        }
        //3.可选关闭线程池，结束后统计时间
        es.shutdown();
        //用于判断线程池是否关闭  同时线程池的任务都结束了
        //es.isTerminated();
        while (!es.isTerminated()) {}
        long end = System.currentTimeMillis();
        System.out.println("所有人进出洞"+(end - start));
    }
}
